﻿#region Libraries

using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

#endregion

namespace XYZ_Inventory
{
    public partial class OrderPayments : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HiddenField date = (HiddenField)DetailsView1.FindControl("hfPaymentDate");
            date.Value = DateTime.Now.ToLongDateString();
        }

        protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            GridView1.DataBind();
        }

        protected void CustomValidator1_OnServerValidate(object source, ServerValidateEventArgs args)
        {
            TextBox tbOrderId = (TextBox)DetailsView1.FindControl("TextBox1");

            if (string.IsNullOrWhiteSpace(tbOrderId.Text)) return;
            int orderId = Convert.ToInt32(tbOrderId.Text);
            XYZDBContext db = new XYZDBContext();
            args.IsValid = db.RawMaterialOrders.SingleOrDefault(x => x.ID == orderId) != null;
        }

        protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            Page.Validate();
            e.Cancel = !Page.IsValid;
        }
    }
}